package com.airticketbooking.controller.user;

import com.airticketbooking.common.constant.JwtClaimsConstant;
import com.airticketbooking.common.domain.Result;
import com.airticketbooking.common.properties.JwtProperties;
import com.airticketbooking.common.utils.JwtUtil;
import com.airticketbooking.common.utils.UserContext;
import com.airticketbooking.dto.UserDTO;
import com.airticketbooking.po.User;
import com.airticketbooking.vo.UserVO;
import com.airticketbooking.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.HashMap;


@RequestMapping("air/user/User")
@RestController
@Api(tags = "用户端用户相关接口")
@RequiredArgsConstructor
@Slf4j

public class UController {


	private final UserService userService;

	private final JwtProperties jwtProperties;
//用户登录接口

	@GetMapping("/login")
	@ApiOperation(value = "用户登录接口")
	public Result<UserVO> login(String username, String password) {


		User user = userService.getUserByUserName(username);
		if (user == null) {
			return Result.error("用户名或密码错误");
		}
		if (!user.getPassword().equals(password)) {
			return Result.error("用户名或密码错误");
		}

		if (user.getStatus() == 0) {
			return Result.error("用户已被禁用");
		}

//		进行jwt token的生成

		HashMap<String, Object> claims = new HashMap<>();
		claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
		String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),
				jwtProperties.getUserTtl(),
				claims);

		UserVO userVO = new UserVO();
		userVO.setUserId(user.getUserId());
		userVO.setUserName(user.getUserName());
		userVO.setToken(token);
		return Result.ok(userVO);
	}

	//	用户注册接口
	@PostMapping("/register")
	@ApiOperation(value = "用户注册接口")
	public Result<String> register(
			@NotBlank(message = "用户名不能为空")
			@Size(min = 3, max = 20, message = "用户名长度必须在3到20个字符之间")
			String userName,

			@NotBlank(message = "密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String password) {

		User userByName = userService.getUserByUserName(userName);

		if (userByName != null) {
			return Result.error("用户名已存在,不能重复注册");
		}

		User user = new User();
		user.setUserName(userName);
		user.setPassword(password);
		userService.add(user);
		return Result.ok("注册成功");
	}

	//	用户修改密码接口
	@GetMapping("/changePassword")
	@ApiOperation(value = "用户修改密码接口")
	public Result<String> changePassword(

			@NotBlank(message = "旧密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String oldPassword,

			@NotBlank(message = "新密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String newPassword,


			@NotBlank(message = "新密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String rePassword) {

		try {
			userService.changePassword(oldPassword, newPassword, rePassword);

			return Result.ok("密码修改成功");

		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}


//	修改用户数据接口

	@PutMapping("/updateUserInfo")
	@ApiOperation(value = "修改用户数据接口")
	public Result<String> updateUserInfo(
			@RequestBody
			UserDTO userDTO) {

		User userById = userService.getUserById(UserContext.getUser().intValue());

		if (userById == null) {
			return Result.error("请先登录");
		}

		userService.modify(userDTO);


		return Result.ok("用户信息修改成功");
	}


	//获取用户信息接口
	@GetMapping("/getUserInfo")
	@ApiOperation(value = "获取用户信息接口")
	public Result<User> getUserInfo() {

		User user = userService.getUserById(UserContext.getUser().intValue());

		return Result.ok(user);

	}

}
